package com.reciver.common.component;

import java.util.Map;
import java.util.Set;
import java.util.List;
import java.util.ArrayList;
import com.reciver.entity.Tbs;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import com.reciver.dto.ZxDevDto;
import javax.annotation.Resource;
import com.reciver.util.DateUtils;
import com.reciver.entity.TbsDbwy;
import com.reciver.entity.TbsDbwy6;
import com.reciver.mapper.TbsMapper;
import com.reciver.common.util.DateUtil;
import com.reciver.mapper.TbsDbwyMapper;
import com.reciver.mapper.TabOnlineMapper;
import com.reciver.mapper.TbsDbwy6Mapper;
import com.reciver.service.ZiXianBeiDouService;
import org.springframework.stereotype.Component;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * @author lgb
 * @date 2020-8-24
 * @说明 定时任务
 **/
@Component @Async 
public class ScheduledService {
	
	@Resource
	private TbsMapper tbsMapper;
	@Resource
	private TbsDbwyMapper tbsDbwyMapper;
	@Resource
	private TbsDbwy6Mapper tbsDbwy6Mapper;
	@Resource
	private TabOnlineMapper tabOnlineMapper;
    @Resource
    private ZiXianBeiDouService zxbdService;
    
	@Scheduled(cron="0 */10 * * * ?")
	public void scheduled() {
		String time = DateUtils.getBeforeDay(30);
		Map<String,String> devIdMap=ZxDevDto.getDevId();
		Set<String> devList = devIdMap.keySet();
		List<TbsDbwy> dbwyList=SpringUtils.getBean(ZiXianBeiDouService.class).findDbwyList(time,0,devList);
		if(!dbwyList.isEmpty()) {
			List<Integer> idList = new ArrayList<>();
			for(TbsDbwy dbwy : dbwyList) {
				idList.add(dbwy.getNum());
				String sensor = devIdMap.get(dbwy.getDevId());
				dbwy.setDevId(sensor);
				zxbdService.insertDbwy(dbwy);
				/** 更新接收数据时间 */
				tabOnlineMapper.updateOnline(ZxDevDto.getId0F03().get(sensor),DateUtil.formatTime());
			}
			zxbdService.updateBtachList(idList);
		}
		List<Map<String,String>> dataSa = tbsDbwyMapper.findSaTime();
		for(Map<String,String> sa:dataSa) {
			tabOnlineMapper.updateOnline(sa.get("dev_id"),sa.get("time"));
		}
		List<Map<String,String>> dataDlQj = tbsDbwyMapper.find0F03Time();
		for(Map<String,String> qj:dataDlQj) {
			tabOnlineMapper.updateOnline(ZxDevDto.getId0F03().get(qj.get("dev_id")),qj.get("time"));
		}
	}
	
	/**
	 * @return 扫描6要素数据 
	 **/
	@Scheduled(cron="0 */1 * * * ?")
	public void scheduled6() {
		String createTime = DateUtils.getBeforeDay(60);
		String deviceNo6 = "ZXBD20200601GK029";
		List<TbsDbwy6> list6=zxbdService.findDbwy6List(createTime,deviceNo6);
		if(!list6.isEmpty()) { 
			/** 更新接收数据时间 */
			tabOnlineMapper.updateOnline(deviceNo6,DateUtil.formatTime());
			for(TbsDbwy6 tbs : list6) {
				Integer count=tbsDbwy6Mapper.findPutCount(tbs.getPutid());
				if(count == 0) {//表示数据库没有 需要插入
					String[] res = tbs.getResult().split(",");
					if(res.length>=6) {
						TbsDbwy6 tbs6 = new TbsDbwy6();
						tbs6.setPutid(tbs.getPutid());
						tbs6.setStationName(tbs.getStationName());
						tbs6.setTemperature(res[0]);
						tbs6.setHumidity(res[1]);
						tbs6.setAtmos(res[2]);
						tbs6.setWinds(res[3]);
						tbs6.setWindd(res[4]);
						String yl=tbsDbwy6Mapper.findYlLastTime();
						BigDecimal byl = new BigDecimal(yl);
						BigDecimal yln = new BigDecimal(res[5]);
						tbs6.setYl(yln.compareTo(byl)==1?yln.subtract(byl).toString():res[5]);
						tbs6.setGpstime(tbs.getGpstime());
						tbs6.setResult(tbs.getResult());
						tbsDbwy6Mapper.insertDbwy6(tbs6);
					}
				}
			}
		}
		/** 水位计 */
		String[] nos={"ZXBD20200601GK034","ZXBD20200601GK032","ZXBD20200601GK025",
				"ZXBD20200601GK033","ZXBD20200601GK028","ZXBD20200601GK027","ZXBD20200601GK026"};
		double[] gao= {(152.099-40.5),(-24+127.845),(-62+135.756),(-22+107.184),
				(-61+143.431),(-42+120.012),182.062,120.012};
		for(int i=0;i<nos.length;i++) {
			String no = nos[i];
			List<TbsDbwy6> noList=zxbdService.findDbwy6List(createTime,no);
			List<Tbs> tbsList = new ArrayList<Tbs>();
			for(TbsDbwy6 tbs : noList) {
				Integer count=tbsMapper.findPutCount("tbs_dxs",tbs.getPutid());
				if(count == 0) {//表示数据库没有 需要插入
					String res = tbs.getResult();
					Tbs entity = new Tbs();
					entity.setPutid(tbs.getPutid());
					entity.setSensorNo(tbs.getStationName());
					double v = Double.parseDouble(res);
					double nv= v+gao[i];
					DecimalFormat df = new DecimalFormat("0.00");
					entity.setValue(df.format(nv));
					entity.setTime(tbs.getGpstime());
					tbsList.add(entity);
				}
			}
			if(!tbsList.isEmpty()) {
				tbsMapper.batchInsertPutTab("tbs_dxs",tbsList);
				tabOnlineMapper.updateOnline(no,DateUtil.formatTime());
			}
		}
	}
	
	/**
	 * @date 2020-9-3 
	 * @return 每天晚上1点钟扫描未被读的数据 去重
	 **/
	@Scheduled(cron="0 0 1 * * ?")
	public void scheduledDay() {
		String time = DateUtils.getBeforeDay(1);
		Map<String,String> devIdMap=ZxDevDto.getDevId();
		Set<String> devList = devIdMap.keySet();
		List<TbsDbwy> dbwyList=SpringUtils.getBean(ZiXianBeiDouService.class).findDbwyList(time,0,devList);
		if(!dbwyList.isEmpty()) {
			List<Integer> idList = new ArrayList<>();
			for(TbsDbwy dbwy : dbwyList) {
				idList.add(dbwy.getNum());
				String sensor = devIdMap.get(dbwy.getDevId());
				dbwy.setDevId(sensor);
				tbsDbwyMapper.insertDbwy(dbwy);
				/** 更新接收数据时间 */
				tabOnlineMapper.updateOnline(sensor,DateUtil.formatTime());
			}
			zxbdService.updateBtachList(idList);
		}
	}
	/**
	 * @date 2020-9-3 
	 * @return 每天晚上1点过3分扫描那些数据为被读取
	 **/
	@Scheduled(cron="0 3 1 * * ?")
	public void scheduledDayNo() {
		String time = DateUtils.getBeforeDay(1);
		Map<String,String> devIdMap=ZxDevDto.getDevId();
		Set<String> devList = devIdMap.keySet();
		List<TbsDbwy> dbwyList=SpringUtils.getBean(ZiXianBeiDouService.class).findDbwyListNoReads(time,devList);
		if(!dbwyList.isEmpty()) {
			List<Integer> idList = new ArrayList<>();
			for(TbsDbwy dbwy : dbwyList) {
				String sensor = devIdMap.get(dbwy.getDevId());
				int count=tbsDbwyMapper.selectBbwyByNum(dbwy.getNum(),sensor);
				if(count==0) {//表示本地数据没有
					idList.add(dbwy.getNum());
					dbwy.setDevId(sensor);
					tbsDbwyMapper.insertDbwy(dbwy);
					/** 更新接收数据时间 */
					tabOnlineMapper.updateOnline(sensor,DateUtil.formatTime());
				}
			}
		}
	}
}